SPA_RESOLVE_INIT = function(transition) {
    getexam()
}

function getexam() {
    $.ajax({
        url:null,//TODO:xxx
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            examId: $.getQueryString("examId")
        },
        success:function (data) {
            if(data.code==200){
                var html = '<div class="right-list-box addexam-box mt-20 magin-auto">\n' +
                    '<div class="items">\n' +
                    '<label>标题：</label>\n' +
                    '<input type="text" class="examName t t1 rad-3 ml-10 f-14" maxlength="20">\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>解锁密码：</label>\n' +
                    '<input type="text" class="unlockSecret t t3 rad-3 ml-10 f-14" maxlength="20">\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>开始时间：</label>\n' +
                    '<input type="date" class="t t7 rad-3 ml-10 f-14" id="startDate" placeholder="格式：2019-10-10"/>\n' +
                    '<input type="time" class="t t7 rad-3 ml-10 f-14" id="startTime" placeholder="格式：18:00"/>\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>结束时间：</label>\n' +
                    '<input type="date" class="t t7 rad-3 ml-10 f-14" id="endDate" placeholder="格式：2019-10-10"/>\n' +
                    '<input type="time" class="t t7 rad-3 ml-10 f-14" id="endTime" placeholder="格式：18:00"/>\n' +
                    '</div>\n' +
                    '<!--自动组卷开始区-->\n' +
                    '<div class="items hide">\n' +
                    '<label>类型：</label>\n' +
                    '<select class="rad-3 t t3 ml-10 f-14">\n' +
                    '<option value="">--请选择--</option>\n' +
                    '<option value="1">填空题</option>\n' +
                    '<option value="2">选择题</option>\n' +
                    '<option value="3">判断题</option>\n' +
                    '<option value="4">程序填空题</option>\n' +
                    '<option value="5">程序改错题</option>\n' +
                    '<option value="6">编程题</option>\n' +
                    '</select>\n' +
                    '<label>难度：</label>\n' +
                    '<select class="rad-3 t t3 ml-10 f-14">\n' +
                    '<option>--请选择--</option>\n' +
                    ' <option value="1">1</option>\n' +
                    '          <option value="2">2</option>\n' +
                    '          <option value="3">3</option>\n' +
                    '          <option value="4">4</option>\n' +
                    '</select>\n' +
                    '</div>\n' +
                    '<div class="items hide">\n' +
                    '<label>分值：</label>\n' +
                    '<input type="text" class="t t3 rad-3 ml-10 f-14" maxlength="3">\n' +
                    '<label>题数：</label>\n' +
                    '<input type="text" class="t t3 rad-3 ml-10 f-14" maxlength="3">\n' +
                    '<a href="javascript:;" class="btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center">增加</a>\n' +
                    '</div>\n' +
                    '<!--自动组卷结束区-->\n' +
                    '<div id="questions" class="questions">' +
                    '<div class="items">\n' +
                    '<label>作业题目：</label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<a href="javascript:;" class="add_question btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="add_question()">增加</a>\n' +
                    '</div>\n' +
                    '</div>' +
                    '<div class="items mt-35">\n' +
                    '<label></label>\n' +
                    '<label></label>\n' +
                    '<a href="javascript:;" class="btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="getexam()">重置</a>\n' +
                    '<label></label>\n' +
                    '<a href="javascript:;" class="btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="updates()">完成编辑</a>\n' +
                    '</div>\n' +
                    '</div> ';
                getHref_path();
                $(".menu ul").hide();
                $(".menu ul").eq(4).show();
                document.getElementById("ui-right-content").innerHTML = html;
                getpaper()
            }
            else {
                $.message(data.msg)
            }

        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });


}

function getpaper() {
    $.ajax({
        url:null,//TODO:xxx
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            paperId: $.getQueryString("paperId")
        },
        success:function (data) {
            if(data.code==200){
                for(var i=0;i<data.data.questions.length;i++){
                    add_question(data.data.questions[i].question,data.data.questions[i].score,i)
                }
            }
            else {
                $.message(data.msg)
            }

        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });

}

function updates() {
    updateExamFun()
    updatePaperFun()
}

function updatePaperFun(){
    var examQuestion=[]
    for(var i=0;i<$(".st").length;i++){
        question=$(".st").eq(i).find(".question").val();
        score=$(".st").eq(i).find(".score").val();
        if(question==""||score==""){
            $.message("请将题号和分值填写完整");
            return;
        }
        examQuestion.push({
            "question":parseInt(question),
            "score":parseInt(score)
        })
    }
    if(examQuestion.length==0){
        $.message("未添加题目");
        return;
    }
    $.ajax({
        url: update_exam_interface,
        type:'post',
        dataType:'json',
        contentType:"application/json",
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:JSON.stringify({
            paperId:$.getQueryString("paperId"),
            paperQuestion:examQuestion
        }),
        success: function(data) {
            if(data.code==200){
                $.message("添加成功");
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

function updateExamFun(paperId=5){
    // var examName = $(".examName").val();
    var unlockSecret = $(".unlockSecret").val();
    // var examStatus =$(".examStatus").val();
    // var courseId = parseInt($("#courseId").val());
    var startTime = $("#startDate").val()+' '+$("#startTime").val();
    var endTime = $("#endDate").val()+' '+$("#endTime").val();
    //预判断
    // if(examName==""){
    //     $.message("请输入考试标题");
    //     return;
    // }
    if(unlockSecret==""){
        $.message("请输入解锁密码");
        return;
    }
    // if(examStatus==""){
    //     $.message("请输入考试状态");
    //     return;
    // }
    examStatus=parseInt(examStatus)
    // if(courseId==""){
    //     $.message("请填写班级ID");
    //     return;
    // }
    if($("#startDate").val()=="" || $("#startTime").val()==""){
        $.message("请填写开始时间");
        return;
    }
    if($("#endDate").val()=="" || $("#endTime").val()==""){
        $.message("请填写结束时间");
        return;
    }
    $.ajax({
        url: add_exam_interface,
        type:'post',
        dataType:'json',
        contentType:"application/json",
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:JSON.stringify({
            examId:$.getQueryString("examId"),
            // examName: examName,
            paperId: $.getQueryString("paperId"),
            startTime: startTime,
            endTime: endTime,
            unlockSecret: unlockSecret,
            // examStatus: examStatus,
            // relateCourse: courseId
        }),
        success: function(data) {
            if(data.code==200){
                $.message("添加成功");
                setTimeout("location.href='#/examList'",500);
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

function deleteQuestionCont(obj) {
    $(obj).closest('.questionCont').remove()
    $(".add_question").eq(-1).show()
}

function add_question(ques="",score="",flag=1) {
    $(".add_question").hide()
    var questionhtml='<div class="items questionCont">\n' +
        '<div class="st">\n' +
        '<label>题号：</label>\n' +
        '<input type="number" class="question t t3 rad-3 ml-10 f-14" value="'+ques+'"/>\n' +
        '<label>分值：</label>\n' +
        '<input type="number" class="score t t3 rad-3 ml-10 f-14" value="'+score+'"/>\n' +
        '</div>\n' +
        '<label>'
    if(flag>0){
        questionhtml+='<a class="pull-left pl-15" onclick="deleteQuestionCont(this)">删除</a>'
    }
    questionhtml+='</label>'+
        '<a href="javascript:;" class="add_question btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="add_question()">增加</a>\n' +
        '</div>\n'
    $("#questions").append(questionhtml)
}
